package lunify;

import java.util.ArrayList;

/**
 *
 * @author katie
 */
public class LUnify {

    public static void run(Source source, String[] args) {

        if (args.length < 5) {
            System.out.println("Insufficient parameters. This program requires at least 5 to me meaningful.\n");
            System.out.println("Usage: <base> <replace> <order> <flags> [processors...]\n\nAvailable processors:\n" + source);
            System.out.println("Flags:\n"
                    + " n (nothing)\n"
                    + " w (remove rows that have relevant elements with whitespace)\n"
                    + " u (remove rows that are duplicates, as determined by relevant elemnts)\n");
            System.exit(1);
        } else {   // Print out usage info
            System.err.println("Usage: <base> <replace> <order> <flags> [processors...]\n\nAvailable processors:\n" + source);
            System.err.println("Flags:\n"
                    + " n (nothing)\n"
                    + " w (remove rows that have relevant elements with whitespace)\n"
                    + " u (remove rows that are duplicates, as determined by relevant elemnts)\n");

        }

        String base = args[0];
        String replace = args[1];
        int[] order;
        boolean w = false;
        boolean u = false;
        {   // check arguments for correctness. Defaults: base=_ replace=_ order=0
            if (base.indexOf(replace) == -1) {
                base = "_";
                replace = "_";
                System.err.println("def.Base:    " + base);
                System.err.println("def.Replace: " + replace);
            } else {
                System.err.println("Base:        " + base);
                System.err.println("Replace:     " + replace);
            }
            {
                ArrayList<Integer> ints = new ArrayList<Integer>();
                String[] temp = args[2].split(",");
                for (String s : temp) {
                    try {
                        ints.add(Integer.parseInt(s.trim()));
                    } catch (Exception e) {
                    }
                }
                if (ints.isEmpty()) {
                    ints.add(0);
                    System.err.println("def.Order:  " + ints);
                } else {
                    System.err.println("Order:      " + ints);
                }
                order = new int[ints.size()];
                for (int i = 0; i < order.length; i++) {
                    order[i] = ints.get(i);
                }
            }
            if (args[4].indexOf("u") != -1) {
                u = true;
            }
            if (args[4].indexOf("w") != -1) {
                w = true;
            }
            System.err.println("Options:     " + (u ? "u" : "") + (w ? "w" : ""));
            System.err.println();
        }

        ArrayList<Rows> dictionaries = new ArrayList<Rows>();
        {   // Read in dictionary arguments
            for (int i = 4; i < args.length; i++) {
                String[] parts = args[i].split("=", 2);
                LProcessor dict = source.processor(parts[0]);
                if (dict != null) {
                    Rows r = dict.process(new FReader(parts[1]));
                    System.err.println(dict + "\n" + r.toString(order) + "\n");
                    dictionaries.add(r);
                } else {
                    System.err.println(parts[0] + ": no such known source");
                }
            }
        }

        // Put all the rows together
        Rows all = new Rows();
        System.err.print("Combining... ");
        long time = System.currentTimeMillis();
        for (Rows one : dictionaries) {
            all.add(one, order);
            System.err.print(".");
        }
        System.out.println(all.toSortedString(w, u, base, replace, order));
        System.err.println("\nCombined\n" + all.toString(order) + "\nTook " + (System.currentTimeMillis() - time) + "ms");
    }
}
